package cn.ynu.cmrs.utils;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.ynu.cmrs.AppConstant;
import cn.ynu.cmrs.model.*;

/**
 * Parse json formatted data into javabean what program can directly use
 * 
 * @author starsfury
 */
public class JsonParse {

	/**
	 * Parse json string, get the state values ​�?and state information
	 * 
	 * @param json
	 *            json string
	 * @return status
	 * @throws JSONException
	 * 
	 */
	public static String[] getStatus(String json) throws JSONException {
		String statusKey;
		String statusMessage = null;
		JSONObject jsonInfo = new JSONObject(json);
		statusKey = String.valueOf(jsonInfo.getInt("status"));
		if (!statusKey.equalsIgnoreCase("0")) {
			statusMessage = jsonInfo.getString("message");
		}
		return new String[] { statusKey, statusMessage };
	}

	/**
	 * Parse json string, get user information
	 * 
	 * @param json
	 *            json string
	 * @return {@link User} object
	 * @throws JSONException
	 */
	public static User getUserInfo(String json) throws JSONException {
		User user;
		String status[] = getStatus(json);
		if (status[0].equals("0")) {
			JSONObject jsonInfo = new JSONObject(json);
			String id = jsonInfo.getString("id");
			String name = jsonInfo.getString("name");
			String icon;
			if (jsonInfo.getString("icon").equals("")) {
				icon = null;
			} else {
				icon = AppConstant.URL.BASE_URL + jsonInfo.getString("icon");
			}
			String email = jsonInfo.getString("email");
			String userInfo = jsonInfo.getString("information");
			String phone = jsonInfo.getString("phone");
			String qq = jsonInfo.getString("qq");
			user = new User(id, name, "", icon, email, userInfo, phone, qq,
					status, "default role");
			return user;
		} else {
			user = new User();
			user.setStatus(status);
			return user;
		}
	}

	/**
	 * Parse json string, get school information
	 * 
	 * @param json
	 *            json string
	 * @return {@link College} object
	 * @throws JSONException
	 */
	public static College getCollege(String json) throws JSONException {
		String status[] = getStatus(json);
		if (status[0].equals("0")) {
			CollegeInfoHead collegeInfoHead;
			List<CollegeInfoItem> items = new ArrayList<CollegeInfoItem>();
			College collegeInfo;
			JSONObject jsonInfo = new JSONObject(json);

			JSONObject head = jsonInfo.getJSONObject("head");
			int total = Integer.parseInt(head.getString("total"));
			collegeInfoHead = new CollegeInfoHead(total);

			JSONArray array = jsonInfo.getJSONArray("items");
			for (int i = 0; i < array.length(); i++) {
				JSONObject item = array.getJSONObject(i);
				String id = item.getString("id");
				String name = item.getString("name");
				String icon;
				if (item.getString("icon").equals("")) {
					icon = null;
				} else {
					icon = AppConstant.URL.BASE_URL + item.getString("icon");
				}
				items.add(new CollegeInfoItem(id, name, icon));
			}
			collegeInfo = new College(collegeInfoHead, items, status);
			return collegeInfo;
		} else {
			return new College(null, null, status);
		}
	}

	/**
	 * Parse json string, get comment information
	 * 
	 * @param json
	 *            json string
	 * @return {@link Comment} object
	 * @throws JSONException
	 */
	public static Comment getComment(String json) throws JSONException {
		String status[] = getStatus(json);
		if (status[0].equals("0")) {
			CommentHead commentHead;
			List<CommentItem> items = new ArrayList<CommentItem>();
			Comment comment;
			JSONObject jsonInfo = new JSONObject(json);

			JSONObject head = jsonInfo.getJSONObject("head");
			int total = Integer.parseInt(head.getString("total"));
			int start = Integer.parseInt(head.getString("start"));
			int amount = Integer.parseInt(head.getString("amount"));
			commentHead = new CommentHead(total, start, amount);

			if (jsonInfo.isNull("items")) {
				items = null;
			} else {
				JSONArray array = jsonInfo.getJSONArray("items");
				for (int i = 0; i < array.length(); i++) {
					JSONObject item = array.getJSONObject(i);
					String commentID = item.getString("id");
					String userID = item.getString("userid");
					String courseID = item.getString("courseid");
					String userName = item.getString("username");
					String userIcon;
					if (item.getString("usericon").equals("")) {
						userIcon = null;
					} else {
						userIcon = AppConstant.URL.BASE_URL
								+ item.getString("usericon");
					}
					String commentContent = item.getString("comment");
					int mark = item.getInt("mark");
					String time = item.getString("time");
					items.add(new CommentItem(commentID, userID, courseID,
							userName, userIcon, commentContent, mark, time));
				}
			}
			comment = new Comment(commentHead, items, status);
			return comment;
		} else {
			return new Comment(null, null, status);
		}
	}

	/**
	 * Parse json string, get course detail information
	 * 
	 * @param json
	 *            json string
	 * @return {@link CourseDetail} object
	 * @throws JSONException
	 */
	public static CourseDetail getCourseDetail(String json)
			throws JSONException {
		CourseDetail courseDetail;
		String status[] = getStatus(json);
		if (status[0].equals("0")) {
			JSONObject jsonInfo = new JSONObject(json);
			String id = jsonInfo.getString("id");
			String name = jsonInfo.getString("name");
			String icon;
			if (jsonInfo.getString("icon").equals("")) {
				icon = null;
			} else {
				icon = AppConstant.URL.BASE_URL + jsonInfo.getString("icon");
			}
			String teacher = jsonInfo.getString("teachername");
			String collegename = jsonInfo.getString("collegename");
			String collegeid = jsonInfo.getString("collegeid");
			int mark = jsonInfo.getInt("mark");
			String[] video;
			if (jsonInfo.isNull("video")) {
				video = null;
			} else {
				JSONArray videos = jsonInfo.getJSONArray("video");
				video = new String[videos.length()];
				for (int i = 0; i < videos.length(); i++) {
					if(videos.getString(i).contains("://"))
					{
						video[i] = videos.getString(i);
					}
					else
					{
						video[i] = AppConstant.URL.RTSP_URL
								+ videos.getString(i);
					}
				}
			}
			String length = jsonInfo.getString("length");
			String address = jsonInfo.getString("address");
			String position = jsonInfo.getString("position");
			String latitude = null;
			String longitude = null;
			if (!position.equals("")) {
				longitude = position.split(",")[1];
				latitude = position.split(",")[0];
			}
			// String attribute = jsonInfo.getString("attribute");
			String information = jsonInfo.getString("information");
			String detailInformation = jsonInfo.getString("detailinformation");
			String[] picture;
			if (jsonInfo.isNull("pictures")) {
				picture = null;
			} else {
				JSONArray pictures = jsonInfo.getJSONArray("pictures");
				picture = new String[pictures.length()];
				for (int j = 0; j < pictures.length(); j++) {
					picture[j] = AppConstant.URL.BASE_URL
							+ pictures.getString(j);
				}
			}
			courseDetail = new CourseDetail(id, name, icon, teacher,
					collegename, collegeid, mark, video, length, address,
					latitude, longitude, information, detailInformation,
					picture, status);
			return courseDetail;
		} else {
			courseDetail = new CourseDetail();
			courseDetail.setStatus(status);
			return courseDetail;
		}
	}

	/**
	 * Parse json string, and get course information
	 * 
	 * @param json
	 *            json string
	 * @return {@link Course} object
	 * @throws JSONException
	 */
	public static Course getCourse(String json) throws JSONException {
		String status[] = getStatus(json);
		if (status[0].equals("0")) {
			CourseInfoHead courseInfoHead;
			List<CourseInfoItem> items = new ArrayList<CourseInfoItem>();
			JSONObject jsonInfo = new JSONObject(json);

			JSONObject head = jsonInfo.getJSONObject("head");
			int total = Integer.parseInt(head.getString("total"));
			int start = Integer.parseInt(head.getString("start"));
			int amount = Integer.parseInt(head.getString("amount"));
			courseInfoHead = new CourseInfoHead(total, start, amount);

			if ((jsonInfo.isNull("items"))) {
				items = null;
			} else {
				JSONArray array = jsonInfo.getJSONArray("items");
				for (int i = 0; i < array.length(); i++) {
					JSONObject item = array.getJSONObject(i);
					String id = item.getString("id");
					String name = item.getString("name");
					String icon;
					if (item.getString("icon").equals("")) {
						icon = null;
					} else {
						icon = AppConstant.URL.BASE_URL
								+ item.getString("icon");
					}
					String teacher = item.getString("teachername");
					String collegename = item.getString("collegename");
					String collegeid = item.getString("collegeid");
					int mark = item.getInt("mark");
					String[] video;
					if (item.isNull("video")) {
						video = null;
					} else {
						JSONArray videos = item.getJSONArray("video");
						video = new String[videos.length()];
						for (int j = 0; j < videos.length(); j++) {
							if(videos.getString(j).contains("://"))
							{
								video[j] = videos.getString(j);
							}
							else
							{
								video[j] = AppConstant.URL.RTSP_URL
										+ videos.getString(j);
							}
						}
					}
					String length = item.getString("length");
					items.add(new CourseInfoItem(id, name, icon, teacher,
							collegename, collegeid, mark, video, length));
				}
			}
			return new Course(courseInfoHead, items, status);
		} else {
			return new Course(null, null, status);
		}
	}
}
